package com.tg.demo.web;

import com.github.pagehelper.PageInfo;
import com.tg.demo.common.*;
import com.tg.demo.entity.Life;
import com.tg.demo.mapper.LifeMapper;
import com.tg.demo.service.LifeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * LifeController
 *
 * @author yuanjin
 * @date 2019/3/29
 */
@Controller
@Slf4j
@RequestMapping("/life")
public class LifeController {

    @Resource
    private LifeService lifeService;

    @Resource
    private LifeMapper lifeMapper;

    /**
     * 跳转到列表页
     *
     * @param model the model
     * @return string string
     */
    @RequestMapping(value = "/gotoListPage", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoListPage(Model model) {
        return "/life/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Page list(Page page, Life life) {
        PageInfo<Life> info = lifeService.getListByPage(life, page.getPage(), page.getPageSize());
        Page result = new Page();
        result.setTotal(Integer.parseInt(info.getTotal() + ""));
        result.setRows(info.getList());
        return result;
    }

    @RequestMapping(value = "/DA", method = {RequestMethod.GET, RequestMethod.POST})
    public String da(Model model) {
        return "/life/view";
    }

    @RequestMapping("/getUpTime")
    @ResponseBody
    public Result getUpTime() {
        List<Auxiliary> res = new ArrayList<>();
        List<String> items = lifeMapper.getItemsByGetUpTime();
        List<Object> keys = new ArrayList<>();
        List<Life> list = lifeService.getListByCondition(Life.builder().build());
        for (String key : items) {
            int count = 0;
            for (Life ele : list) {
                if (key.equals(ele.getGetUpTime())) {
                    count++;
                }
            }
            keys.add(key.substring(0,5));
            res.add(new Auxiliary(count, key.substring(0,5)));
        }
        PieView view = new PieView(keys, res);
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(view);
        return result;
    }

    @RequestMapping("/breakfastTime")
    @ResponseBody
    public Result breakfastTime() {
        List<Auxiliary> res = new ArrayList<>();
        List<String> items = lifeMapper.getItemsByBreakfastTime();
        List<Object> keys = new ArrayList<>();
        List<Life> list = lifeService.getListByCondition(Life.builder().build());
        for (String key : items) {
            int count = 0;
            for (Life ele : list) {
                if (key.equals(ele.getBreakfastTime())) {
                    count++;
                }
            }
            keys.add(key.substring(0,5));
            res.add(new Auxiliary(count, key.substring(0,5)));
        }
        PieView view = new PieView(keys, res);
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(view);
        return result;
    }

    @RequestMapping("/sleepTime")
    @ResponseBody
    public Result sleepTime() {
        List<Auxiliary> res = new ArrayList<>();
        List<String> items = lifeMapper.getItemsBySleepTime();
        List<Object> keys = new ArrayList<>();
        List<Life> list = lifeService.getListByCondition(Life.builder().build());
        for (String key : items) {
            int count = 0;
            for (Life ele : list) {
                if (key.equals(ele.getSleepTime())) {
                    count++;
                }
            }
            keys.add(key.substring(0,5));
            res.add(new Auxiliary(count, key.substring(0,5)));
        }
        PieView view = new PieView(keys, res);
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(view);
        return result;
    }

    @RequestMapping("/breakfastCount")
    @ResponseBody
    public Result breakfastCount() {
        List<Integer> items = lifeMapper.getItemsByBreakfastCount();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"30次以下", "30-70次", "70-100次", "100次以上"};
        for (int i = 0; i < arr.length; i++) {
            keys.add(arr[i]);
            int count = 0;
            for (Integer ele : items) {
                if (i == 0 && ele < 30) {
                    count++;
                } else if (i == 1 && ele >= 30 && ele < 70) {
                    count++;
                } else if (i == 2 && ele >= 70 && ele < 100) {
                    count++;
                } else if (i == 3 && ele >= 100) {
                    count++;
                }
            }
            values.add(count);
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/shoppingCount")
    @ResponseBody
    public Result shoppingCount() {
        List<Integer> items = lifeMapper.getItemsByShoppingCount();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"5次以下", "5-10次", "10-15次","15-20次", "20次以上"};
        for (int i = 0; i < arr.length; i++) {
            keys.add(arr[i]);
            int count = 0;
            for (Integer ele : items) {
                if (i == 0 && ele < 5) {
                    count++;
                } else if (i == 1 && ele >= 5 && ele < 10) {
                    count++;
                } else if (i == 2 && ele >= 10 && ele < 15) {
                    count++;
                } else if (i == 3 && ele >= 15 && ele < 20) {
                    count++;
                } else if (i == 4 && ele >= 20) {
                    count++;
                }
            }
            values.add(count);
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/money")
    @ResponseBody
    public Result money() {
        List<Double> items = lifeMapper.getItemsByMoney();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"1000元以下", "1000-2000元", "2000-3000元","3000元-4000元", "4000元以上"};
        for (int i = 0; i < arr.length; i++) {
            keys.add(arr[i]);
            int count = 0;
            for (Double ele : items) {
                if (i == 0 && ele < 1000) {
                    count++;
                } else if (i == 1 && ele >= 1000 && ele < 2000) {
                    count++;
                } else if (i == 2 && ele >= 2000 && ele < 3000) {
                    count++;
                } else if (i == 3 && ele >= 3000 && ele < 4000) {
                    count++;
                } else if (i == 4 && ele >= 4000) {
                    count++;
                }
            }
            values.add(count);
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }
}
